package br.com.goals.tableedit.util;

import java.util.ArrayList;

import br.com.goals.tableedit.TableEdit;
import br.com.goals.tableedit.model.FieldConfig;
import br.com.goals.tableedit.model.TableToHtml;

/**
 * Cria o formulario para adicionar
 * 
 * @author Fabio Issamu Oshiro
 * 
 */
public class FormFactory {
	/**
	 * Cria o formulario na vertical
	 * @return String com o codHtml
	 */
	public static String criaVertical(TableToHtml tabela){	
		return criaForm(tabela,true,null);
	}
	
	/**
	 * Cria o formulario na vertical
	 * @return String com o codHtml
	 */
	public static String criaVertical(TableToHtml tabela,String btnExtra){	
		return criaForm(tabela,true,btnExtra);
	}
	/**
	 * Cria o formulario na vertical
	 * @return String com o codHtml
	 */
	public static String criaVerticalUpdSingle(TableToHtml tabela){		
		return criaForm(tabela,false,null);
	}
	
	/**
	 * Cria o formulario na vertical
	 * @param tabela metadados para gerar o formulario
	 * @param btnExtra botao extra para colocar na area de botoes
	 * @return HTML
	 */
	public static String criaVerticalUpdSingle(TableToHtml tabela,String btnExtra){		
		return criaForm(tabela,false,btnExtra);
	}
	
	/**
	 * Rotina interna
	 * @param tabela metadados para gerar o formulario
	 * @param defaultValue
	 * @param btnExtra botao extra para colocar na area de botoes
	 * @return HTML
	 */
	private static String criaForm(TableToHtml tabela,boolean defaultValue,String btnExtra){
		ArrayList<FieldConfig> listFieldConfig =tabela.getListFieldConfig(); 
		StringBuilder sb = new StringBuilder();
		
		for (FieldConfig fieldConfig:listFieldConfig) {
			if(fieldConfig.isAutoIncrement){
				if (defaultValue){
					sb.append(FormFieldFactory.createHidden(fieldConfig,"0")+"\n");
				}else{
					sb.append(FormFieldFactory.createHidden(fieldConfig,fieldConfig.singleValue)+"\n");
				}
			}else if(fieldConfig.isHidden){
				sb.append(FormFieldFactory.createHidden(fieldConfig, fieldConfig.singleValue));
			}else if(fieldConfig.dontHtml){
				//do nothing
			}else{
				sb.append("<div class=\""+ TableEdit.CSS_PREFIX +"_row\">");
				if (!fieldConfig.isEditable) {
					sb.append("<div class=\"lbl\">");
					sb.append(fieldConfig.displayName);
					sb.append(":</div>");
				}else{
					sb.append("<label for=\"");
					sb.append(fieldConfig.htmlName);
					sb.append("\">");
					sb.append(fieldConfig.displayName);
					sb.append(":</label>");
				}
				if(fieldConfig.htmlCampoDoFormulario!=null){
					sb.append(fieldConfig.htmlCampoDoFormulario);
				}else if (defaultValue){
					sb.append(FormFieldFactory.createEditable(fieldConfig,fieldConfig.defaultValue));
				}else{
					sb.append(FormFieldFactory.createEditable(fieldConfig,fieldConfig.singleValue));
				}
				sb.append("</div>\n");
			}
		}
		sb.append("<div class=\""+ TableEdit.CSS_PREFIX +"_btns\">");
		if(tabela.comDelete){
			sb.append("<br />Deseja mesmo apagar este registro?<br />");
			sb.append("<input type=\"submit\" name=\"btn_delete\" value=\"Apagar\" />");
			sb.append(" <input type=\"submit\" name=\"btn_cancel\" value=\"Cancelar\" />");
		}else{
			if(tabela.toAdd){
				sb.append("<br/><input type=\"submit\" value=\"Cadastrar\" />");
			}else{
				sb.append("<br/><input type=\"submit\" value=\"Atualizar\" />");
			}
			sb.append(" <input type=\"reset\" value=\"Limpar\" />");
		}
		if(btnExtra!=null){
			sb.append(" ");
			sb.append(btnExtra);
		}
		sb.append("</div>\n");
		return sb.toString();
	}
}
